/**
 * Class for representing the asteroid object
 * 
 * @author Luke Brown, Shane Champion, Jordan May, Guo Wang
 */

import java.awt.Graphics2D;
import java.awt.Color;

public class Asteroid implements Paintable
{
        double r;                       //radius of asteroid
        double x;                          //x coordinate
        double y;                          //y coordinate
        double m;                       //mass of asteroid (based on r^2)
        int om;                 //original mass of asteroid
        double vx;                      //x velocity
        double vy;                      //y velocity
        Environment e;

        /**
         * Constructor for the asteroid class
         * 
         * @param r radius
         * @param x x
         * @param y y
         * @param vx x velocity
         * @param vy y velocity
         */
        public Asteroid(double r, int x, int y, double vx, double vy,Environment e)
        {
                this.r=r;
                this.x=x;
                this.y=y;
                this.vx=vx;
                this.vy=vy;
                this.m=r*r;
                this.om= (int)(r*r);
                this.e=e;
        }
        /**
         * Damages the asteroid based on mass of missile
         * 
         * @param collideM mass of colliding object
         */
        public void shatter(double collideM,double bvx,double bvy)
        {
                m-=collideM;
                r=Math.pow(r*r-collideM,1.0/2);
                vx=(m*vx+collideM*bvx)/m;
                vy=(m*vy+collideM*bvy)/m;
        }
        /**
         * Moves the asteroid based on x and y velocity
         */
        public void move()
        {
				if (vy<1.5) vy+=0.05;
				if (vx>0 || vx<0) vx*=0.975;
                x+=vx;
                y+=vy;
                if(y>480+r){
                        e.setHealth(e.getHealth()-(int)this.m);
                        e.getaList().remove(this);
                }
                for(int i =0;i<e.getbList().size()-1;i++){
                        if(Math.pow(x-e.getbList().get(i).getX(),2)+Math.pow(y-e.getbList().get(i).getY(),2)<=Math.pow(r+e.getbList().get(i).getR(),2)){
								int bvx=e.getbList().get(i).getVx();
								int bvy=e.getbList().get(i).getVy();
                                shatter(30,bvx,bvy);
                                e.setHitBullets(e.getHitBullets()+1);
                                //System.out.println(e.getHitBullets());
                                e.getbList().remove(i);

                        }
                }
                if(this.r<3){
                        e.getaList().remove(this);
                        int score = e.getScore();
                        score+= om;
                        e.setScore(score);
                }
                if (x>=640) x=0;
                else if (x<0) x+=640;
        }
        /**
         * Returns r
         * 
         * @return r
         */
        public double getR()
        {
                return r;
        }
        /**
         * Returns x
         * 
         * @return x
         */
        public double getX()
        {
                return x;
        }
        /**
         * Returns y
         * 
         * @return y
         */
        public double getY()
        {
                return y;
        }
        /**
         * Returns m
         * 
         * @return m
         */
        public double getM()
        {
                return m;
        }
        /**
         * Returns vx
         * 
         * @return vx
         */
        public double getVX()
        {
                return vx;
        }
        /**
         * Returns vy
         * 
         * @return vy
         */
        public double getVY()
        {
                return vy;
        }
        /**
         * Sets r to the specified value
         * 
         * @param r r
         */
        public void setR(double r)
        {
                this.r=r;
        }
        /**
         * Sets x to the specified value
         * 
         * @param x x
         */
        public void setX(int x)
        {
                this.x=x;
        }
        /**
         * Sets y to the specified value
         * 
         * @param y y
         */
        public void setY(int y)
        {
                this.y=y;
        }
        /**
         * Sets m to the specified value
         * 
         * @param m m
         */
        public void setM(double m)
        {
                this.m=m;
        }
        /**
         * Sets vx to the specified value
         * 
         * @param vx vx
         */
        public void setVX(double vx)
        {
                this.vx=vx;
        }
        /**
         * Sets vy to the specified value
         * 
         * @param vy vy
         */
        public void setVY(double vy)
        {
                this.vy=vy;
        }
        /**
         * Draws the asteroid
         * 
         * @param g g
         */
        public void Paint(Graphics2D g)
        {
                g.setColor(new Color(160,120,10));
                g.fillOval((int)this.x-(int)this.r, (int)this.y-(int)this.r, 2*(int)this.r, 2*(int)this.r);
        }
}
